<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Texts extends Admin_Controller {
	public function __construct()
	{
		parent::Admin_Controller();
		$this->config->load('cirpho');
		$this->load->helper('form');
		$this->lang->load('cirpho');
		$this->lang->load('texts');
		$this->load->model('cip_texts_m');
		/*
		$this->template
			->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
			->append_metadata( css('cirpho.css', 'cirpho') )
			->set_partial('shortcuts', 'admin/partials/shortcuts');
		*/
		$this->template
		->set_partial('shortcuts', 'admin/partials/shortcuts')
		->append_metadata(js('widgets.js', 'widgets'))
		->append_metadata(css('widgets.css', 'widgets'))
		->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE));
		
		// Set the validation rules
		$this->load->library('form_validation');
		$this->validation_rules = array(
			array(
				'field' => 'how_to_use',
				'label' => lang('cirpho_text_how_to_use'),
				'rules' => 'trim|required|max_length[3000]'
			)
		);
		$this->form_validation->set_rules($this->validation_rules);
	}
	public function index()
	{
		$how_to_use = $this->cip_texts_m->get('how_to_use');
		// Validate the data
		if ($this->form_validation->run())
		{
			//echo $_POST['how_to_use'];
			//die();
			$isSuccess = $this->cip_texts_m->update($how_to_use->id, $_POST['how_to_use']);
			
			if($isSuccess){
				$this->session->set_flashdata('success', lang('cirpho_text_update_success'));
			}else{
				$this->session->set_flashdata('error', lang('cirpho_text_update_error'));
			}
				
			redirect('admin/cirpho/texts');
		}
		$this->pyrocache->delete_all('modules_m');
		
		$this->template
			->title($this->module_details['name'], lang('cirpho_text_title'))
			->set('how_to_use', $how_to_use)
			->build('admin/texts/form', $this->data);
	}
	
	/**
	 * Callback method that checks the title of the category
	 * @access public
	 * @param string title The title to check
	 * @return bool
	 */
	public function _check_title($name = '')
	{
		if ($this->cip_owners_m->check_title($name))
		{
			$this->form_validation->set_message('_check_title', sprintf($this->lang->line('cirpho_owner_already_exist_error'), $name));
			return FALSE;
		}

		return TRUE;
	}
}